package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3EdittextWatcher;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;

import razerdp.basepopup.BasePopupWindow;

public class X3BottomAddRichDialog extends BasePopupWindow implements View.OnClickListener {

    private EditText et_input;
    private ImageView iv_empty;
    private X3SwipeBackLayout sl_view;
    private TextView tv_cancel;
    private Button btn_create;
    private Context mContext;

    public X3BottomAddRichDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.x3_bottom_add_rich_dialog);
    }

    @Override
    public void onViewCreated(@NonNull View contentView) {
        super.onViewCreated(contentView);
        et_input = findViewById(R.id.et_input);
        iv_empty = findViewById(R.id.iv_empty);
        tv_cancel = findViewById(R.id.tv_cancel);
        sl_view = findViewById(R.id.sl_view);
        btn_create = findViewById(R.id.btn_create);

        et_input.addTextChangedListener(new X3EdittextWatcher() {
            @Override
            protected void textInputAfter(String content) {
                iv_empty.setVisibility(X3StringUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
                btn_create.setEnabled(X3StringUtils.isEmpty(content) ? false : true);
                btn_create.setBackground(X3StringUtils.isEmpty(content) ? ContextCompat.getDrawable(mContext,R.drawable.x3_shape_rad_4_cor_d5d5)
                        : ContextCompat.getDrawable(mContext,R.drawable.x3_alone_dialog_affirm));
            }
        });

        iv_empty.setOnClickListener(this);
        btn_create.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);

        sl_view.setIsBackToEnd(false);
        sl_view.setSwipeBackListener(new X3SwipeBackLayout.OnSwipeBackListener() {
            @Override
            public void onViewPositionChanged(View mView, float swipeBackFraction, float swipeBackFactor) {
                if (swipeBackFraction >= 0.5) {
                    onViewSwipeFinished(mView,true);
                }
            }

            @Override
            public void onViewSwipeFinished(View mView, boolean isEnd) {
                if (isEnd) {
                    dismiss();
                }
            }

            @Override
            public void onViewBack() {
                // 下拉弹回
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_empty) {
            et_input.setText("");
        } else if (id == R.id.btn_create) {
            String tag = et_input.getText().toString().trim();
            if (X3StringUtils.isEmpty(tag)) {
                X3ToastUtils.showMessage(mContext.getString(R.string.x3_no_input_trim));
                return;
            }
            if (iOnSelectRich != null) {
                iOnSelectRich.iOnSelectRich(tag);
            }
            dismiss();
        }else if (id == R.id.tv_cancel) {
            dismiss();
        }
    }

    IOnSelectRich iOnSelectRich;

    public X3BottomAddRichDialog setiOnSelectTag(IOnSelectRich iOnSelectRich) {
        this.iOnSelectRich = iOnSelectRich;
        return this;
    }

    public interface IOnSelectRich {
        void iOnSelectRich(String content);
    }

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        iv_empty.performClick();
    }

    @Override
    public void dismiss() {
        X3StringUtils.hideSoftInput(et_input);
        super.dismiss();
    }
}
